package cn.wen233.srpc.core.execute;

import cn.wen233.srpc.basic.exception.BaseException;
import cn.wen233.srpc.basic.execute.Executor;
import cn.wen233.srpc.basic.filter.ClientFilter;
import cn.wen233.srpc.basic.filter.ServerFilter;
import cn.wen233.srpc.basic.model.Request;
import cn.wen233.srpc.basic.model.Response;
import cn.wen233.srpc.core.service.spi.SPIContext;
import cn.wen233.srpc.core.utils.ServiceLoaderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Server Abstract Executor
 *
 * @author wenei
 * @date 2021-10-28 16:21
 */
public interface ServerExecutor extends Executor {

    /**
     * 子类实现具体实现
     * @param request 请求信息
     * @return 响应数据
     */
    Response doExecute(Request request);

    @Override
    default Response execute(Request request) {
        for (ServerFilter filter : SPIContext.loadService(ServerFilter.class)) {
            if (!filter.filter(request)) {
                return Response.REFUSE;
            }
        }
        return doExecute(request);
    }
}
